package com.lzj.demo.design.adapter;

import cn.hutool.json.JSONUtil;
import com.lzj.demo.design.adapter.dto.RebateInfo;

import java.util.Map;

/**
 * MQ适配
 * */
public class MqAdapter {
    public static RebateInfo adapter(String strJson, Map<String,String> link) throws Exception{
        return doAdapter(JSONUtil.toBean(strJson,Map.class),link);
    }
    /**
     * 使用反射技术实现适配
     * */
    public static RebateInfo doAdapter(Map map,Map<String,String> link) throws Exception{
        RebateInfo rebateInfo = new RebateInfo();
        for(String key:link.keySet()){
            Object obj = map.get(link.get(key));
            RebateInfo.class
                    .getMethod("set"+key.substring(0,1).toUpperCase()+ key.substring(1),String.class)
                    .invoke(rebateInfo,obj.toString());
        }
        return rebateInfo;
    }
}
